package com.tank.server;

import com.alibaba.fastjson.JSON;
import com.tank.protobuf.DataInfo;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/5/29 0029
 * Time: 上午 11:37
 * To change this template use File | Settings | File Templates.
 */
public class ChatServerHandler extends SimpleChannelInboundHandler<DataInfo.ParentMessag> {

    //保存链接对象
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    //读写时间
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DataInfo.ParentMessag msg) throws Exception {
        if(msg.getDataType() == DataInfo.ParentMessag.DateType.StudentType){
            System.out.println(String.format("receive message:%s", msg.getStudent().getName()));
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();;
        ctx.close();
    }

}
